package com.photoeditor.demo.ui.widget.face;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.photoeditor.R;
import com.photoeditor.demo.model.subscribe.manager.AppVipManager;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * desc：
 * Created by chenli on 2018/11/9
 */
public class AgeTextView extends FrameLayout {
    @Bind(R.id.tv_age_number)
    TextView tvNumber;
    @Bind(R.id.tv_age_desc)
    TextView tvDesc;
    @Bind(R.id.img_vip)
    ImageView imgVip;

    FrameLayout mFrameLayout;

    public AgeTextView(Context context) {
        super(context);
        init(null);
    }

    public AgeTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public AgeTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.AgeTextView, 0, 0);
        String ageNumber = typedArray.getString(R.styleable.AgeTextView_ageNumber);
        String ageDesc = typedArray.getString(R.styleable.AgeTextView_ageDesc);
        boolean isSelect = typedArray.getBoolean(R.styleable.AgeTextView_isSelect, false);
        typedArray.recycle();

        //填充布局
        View view = ((LayoutInflater) getContext().getSystemService
                (Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.view_age_select_status, null);
        view.setLayoutParams(new ViewGroup.LayoutParams
                (ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        this.addView(view);
        ButterKnife.bind(this, view);


        tvNumber.setText(ageNumber);
        tvDesc.setText(ageDesc);
        tvNumber.setSelected(isSelect);
        tvNumber.setTextColor(isSelect ? getResources().getColor(R.color.color_text_age_btns) :
                getResources().getColor(R.color.color_text_age_des_unsel));
        tvDesc.setTextColor(isSelect ? getResources().getColor(R.color.color_text_age_des) :
                getResources().getColor(R.color.color_text_age_des_unsel));
        tvDesc.setTextSize(isSelect ? 14 : 12);
        //
        selectGender(isSelect);
        if (AppVipManager.isVip()) {
            imgVip.setVisibility(GONE);
        } else {
            imgVip.setVisibility(VISIBLE);
        }
    }

    public void updateVip(boolean isVip) {
        if (isVip) {
            imgVip.setVisibility(GONE);
        } else {
            imgVip.setVisibility(VISIBLE);
        }
    }

    @Override
    public void setSelected(boolean selected) {
        super.setSelected(selected);
        tvNumber.setSelected(selected);
        tvNumber.setTextColor(selected ? getResources().getColor(R.color.color_text_age_btns) :
                getResources().getColor(R.color.color_text_age_des_unsel));
        tvDesc.setTextColor(selected ? getResources().getColor(R.color.color_text_age_des) :
                getResources().getColor(R.color.color_text_age_des_unsel));
        tvDesc.setTextSize(selected ? 14 : 12);
        //
        selectGender(selected);
    }

    private void selectGender(boolean isSelect) {
        if (tvDesc.getText().equals(getResources().getString(R.string.gender_swap_male))) {
            setDrawableToTextViewLeft(tvNumber, isSelect ? R.drawable.ic_male_select : R.drawable.ic_male_unselect);
        } else if (tvDesc.getText().equals(getResources().getString(R.string.gender_swap_female))) {
            setDrawableToTextViewLeft(tvNumber, isSelect ? R.drawable.ic_female_select : R.drawable.ic_female_unselect);
        }
    }

    private void setDrawableToTextViewLeft(TextView tv, int resId) {
        Drawable weather = getResources().getDrawable(resId);
        weather.setBounds(15, 0, 15 + weather.getMinimumWidth(), weather.getMinimumWidth());
        tv.setCompoundDrawables(weather, null, null, null);

    }
}
